﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcEnrollment.Models;
using MvcEnrollment.Helpers;
using System.Collections;

namespace MvcEnrollment.Controllers
{
    public class DashboardController : Controller
    {
        private EnrollmentEntities1 edb = new EnrollmentEntities1();

        public ActionResult Enrollments() {
            var studentEnrollments = new List<Orders>();

            if (edb != null && edb.Students != null && edb.Courses != null && edb.Orders != null && edb.Payments != null && edb.Transactions != null)
            {
                studentEnrollments = (from order in edb.Orders
                                      from payment in edb.Payments
                                      from transaction in edb.Transactions
                                      from course in edb.Courses
                                      from coursePrice in edb.Course_Price
                                      from student in edb.Students
                                      where order.Order_Id == payment.Order_Id &&
                                            payment.Payments_Id == transaction.Payments_Id &&
                                            order.Course_Id == course.Course_Id &&
                                            course.Course_Id == coursePrice.Course_Id &&
                                            order.Student_Id == student.Student_Id
                                      select new Orders
                                      {
                                          Student_Id = student.Student_Id,
                                          Title = student.Title,
                                          First_Name = student.First_Name,
                                          Last_Name = student.Last_Name,
                                          Birthday = student.Birthday,
                                          Phone = student.Phone,
                                          Email = student.Email,
                                          Address = student.Address,
                                          Postcode = student.Postcode,
                                          State = student.State,
                                          Country = student.Country,
                                          Course_Id = course.Course_Id,
                                          Course_Name = course.Course_Name,
                                          Course_Provider = course.Course_Provider,
                                          Course_Description = course.Course_Description,
                                          Study_Mode = course.Study_Mode,
                                          Duration = course.Duration,
                                          Career_Prospect = course.Career_Prospect,
                                          Recognition_Accredition = course.Recognition_Accredition,
                                          Qualification_Prerequisites = course.Qualification_Prerequisites,
                                          Testimonial = course.Testimonial,
                                          Units = course.Units,
                                          Provider_Id = course.Provider_Id
                                      }).ToList<Orders>();
            }
            
            return View(edb.Students);
        }
        /*
        private CourseDBContext db = new CourseDBContext();
        // GET: /Dashboard/

        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

        // GET: /Dashboard/Direct

        public ActionResult Direct()
        {
            var findP = from p in db.Students
                        select p;
            findP = findP.Where(s => s.Payment.PaymentOption.Equals("Full"));
            return View(findP.ToList());
        }

        // GET: /Dashboard/Recurring

        public ActionResult Recurring()
        {
            var findP = from p in db.Students
                        where (p.Payment.PaymentOption.Equals("Month") || p.Payment.PaymentOption.Equals("Week"))
                        select p;
            return View(findP.ToList());
        }

        // GET: /Dashboard/Details

        public ActionResult Details(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // GET: /Dashboard/DirectTransaction

        public ActionResult DirectTransaction(string id)
        {
            var findP = from p in db.Transactions
                        where (p.TrxnReference == id)
                        select p;
            Transaction direct = findP.SingleOrDefault();
            if (direct == null)
            {
                return HttpNotFound();
            }

            return View(direct);
        }

        // GET: /Dashboard/RecurringTransaction

        public ActionResult RecurringTransaction(string id)
        {
            int idInt = 0;
            int.TryParse(id, out idInt);
            Student student = db.Students.Find(idInt);
            if (student == null)
            {
                return HttpNotFound();
            }

            HttpCookie hc = new HttpCookie("studentID", id);
            Response.Cookies.Add(hc);

            var soap = new ewayRecurringWebService.manageRebillSoapClient("manageRebillSoap");
            soap.Open();
            var header = new ewayRecurringWebService.eWAYHeader();
            header.eWAYCustomerID = "87654321";
            header.Username = "test@eway.com.au";
            header.Password = "test123";
            var temp = soap.QueryTransactions(header, student.RebillCustomerID, student.RebillId, null, null, null);
            var transaction = new ewayRecurringWebService.rebillTransaction[temp.Length];
            transaction = temp;
            soap.Close();
            return View(transaction);
            
        }

        // GET: /Dashboard/NextDetails

        public ActionResult NextDetails(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // GET: /Dashboard/Next

        public ActionResult NextTransaction(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }

            var soap = new ewayRecurringWebService.manageRebillSoapClient("manageRebillSoap");
            soap.Open();
            var header = new ewayRecurringWebService.eWAYHeader();
            header.eWAYCustomerID = "87654321";
            header.Username = "test@eway.com.au";
            header.Password = "test123";

            var transaction = new ewayRecurringWebService.rebillTransaction();
            transaction = soap.QueryNextTransaction(header, student.RebillCustomerID, student.RebillId);

            soap.Close();

            return View(transaction);
        }

        // GET: /Dashboard/UpdateRebillEvent

        public ActionResult UpdateRebillEvent(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }

            var soap = new ewayRecurringWebService.manageRebillSoapClient("manageRebillSoap");
            soap.Open();
            var header = new ewayRecurringWebService.eWAYHeader();
            header.eWAYCustomerID = "87654321";
            header.Username = "test@eway.com.au";
            header.Password = "test123";

            var rebillEvent = new ewayRecurringWebService.RebillEventDetails();
            rebillEvent = soap.QueryRebillEvent(ref header, student.RebillCustomerID, student.RebillId);

            soap.Close();

            return View(rebillEvent);
        }

        // POST: /Dashboard/UpdateRebillEvent

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateRebillEvent(ewayRecurringWebService.RebillEventDetails rebillEvent)
        {
            var soap = new ewayRecurringWebService.manageRebillSoapClient("manageRebillSoap");
            soap.Open();
            var header = new ewayRecurringWebService.eWAYHeader();
            header.eWAYCustomerID = "87654321";
            header.Username = "test@eway.com.au";
            header.Password = "test123";

            var updateRebillEvent = new ewayRecurringWebService.RebillEventDetails();
            updateRebillEvent = soap.UpdateRebillEvent(ref header, rebillEvent.RebillCustomerID, rebillEvent.RebillID, 
                rebillEvent.RebillInvRef, rebillEvent.RebillInvDesc, rebillEvent.RebillCCName, rebillEvent.RebillCCNumber,
                Request.Form["exM"], Request.Form["exY"], rebillEvent.RebillInitAmt, rebillEvent.RebillInitDate, rebillEvent.RebillRecurAmt,
                rebillEvent.RebillStartDate, Request.Form["interval"], Request.Form["interType"], rebillEvent.RebillEndDate);

            soap.Close();

            if (updateRebillEvent.Result.Equals("Success"))
            {
                TempData["SuccessMsg"] = "The rebill event has been updated successfully!";
                return RedirectToAction("Details", new { id = Request.Cookies["studentID"].Value});
            }
            ModelState.AddModelError("UpdateError", updateRebillEvent.ErrorDetails);
            return View(rebillEvent);
        }*/
    }
}
